<template>
    <div class="user-container">
      <!-- 用户基本信息 -->
      <div class="user-card">
        <!-- 用户头像、姓名 -->
        <van-cell>
          <!-- 头像 -->
          <template #icon>
            <img :src="$store.state.user.photo" alt="">
          </template>
          <!-- 标题姓名 -->
          <template #title>
            <span class="username">{{$store.state.user.name}}</span>
          </template>
          <!-- label内容 -->
          <template #label>
            <van-tag color="#fff" text-color="#007bff">申请认证</van-tag>
          </template>
        </van-cell>
        <!-- 动态、关注、粉丝 -->
        <div class="user-data">
          <div class="user-data-item">
            <div>{{userinfo.art_count}}</div>
            <div>动态</div>
          </div>
          <div class="user-data-item">
            <div>{{userinfo.follow_count}}</div>
            <div>关注</div>
          </div>
          <div class="user-data-item">
            <div>{{userinfo.fans_count}}</div>
            <div>粉丝</div>
          </div>
        </div>
      </div>

      <!-- 用户操作面板 -->
      <div class="action-card">
        <van-cell-group>
          <van-cell title="编辑资料" is-link icon="edit" to="/userEdit"></van-cell>
          <van-cell title="小思同学" is-link icon="chat-o" to="/chat"></van-cell>
          <van-cell title="退出登录"  icon="warning-o" @click="quitLogin"></van-cell>
        </van-cell-group>
      </div>
    </div>
</template>

<script>
import { getUserAPI } from '@/api'
import { Dialog } from 'vant'
import { mapMutations } from 'vuex'
import { setStorage, removeStorage } from '@/utils/Storage'
export default {
  name: 'myUser',
  data () {
    return {
      userinfo: {}
    }
  },
  created () {
    this.getUser()
    console.log(this.$store.state.user)
  },
  methods: {
    ...mapMutations(['SET_USER']),
    // 获取用户信息
    async getUser () {
      const { data: res } = await getUserAPI()
      // console.log(res)
      this.userinfo = res.data
      // 将用户信息保存到state中
      this.SET_USER(res.data)
      setStorage('user', res.data)
    },

    // 退出登录
    quitLogin () {
      // 弹出对话框，确认
      Dialog.confirm({
        title: '退出登录',
        message: '确认退出登录吗？'
      })
        .then(() => {
          // on confirm
          // 退出登录 ，删除本地token
          removeStorage('key_token')
          // 跳转路由到首页
          this.$router.push('/login')
        })
        .catch(() => {
          console.log(333)
          // on cancel
        })
    }
  }
}
</script>

<style lang="less" scoped>

.user-container{
  .user-card{
    background-color: #007bff;
    color: #fff;
    padding-top: 20px;
    .van-cell{
      background-color: #007bff;
      color: #fff;
      // 让底部的边框消失
      &::after{
        display: none;
      }
      img{
      width: 60px;
      height: 60px;
      background-color: #fff;
      margin-right:13px;
      border-radius: 50%;
      }
      .username{
        font-size: 14px;
        font-weight: bold;
      }
    }
    .user-data{
      display: flex;
      justify-content: space-evenly;
      align-items: center;
      font-size: 14px;
      padding: 30px 0;
      .user-data-item{
        width: 33.33%;
         display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
      }
    }
  }
}
</style>
